<?php
namespace app\mobile\validate;
use think\Validate;
use think\Db;
class Company extends Validate
{
	
	//验证规则
	protected $rule = [
		'mobile'=>'require|mobile',
		'name' => 'min:6|max:35|chsAlphaNum|checkCompanyname:',
		'password' => 'require|password',
		'verify' => 'require|number|length:4|checkVerify:',	
		'repassword' => 'require|password|confirm:password',
		'platform' => 'require|in:1,2',
	];
		
	
	//提示信息
	protected $message = [
		'mobile.require' => '手机号码必须填写',
		'mobile.mobile' => '手机号码格式错误',
				
		'name.min' => '公司名称最少不低于6个字符',
		'name.max' => '公司名称最多不超过35个字符',
		'name.chsAlphaNum' => '公司名称格式错误',
				
		'password.require' => '密码必须填写',
		'password.password' => '密码格式6-18个字母数字符号组合',
		
		'verify.require' => '验证码必须填写',
		'verify.number' => '验证码格式错误',
		'verify.length' => '验证码为4位',
		
		'repassword.require' => '确认密码必须填写',
		'repassword.password' => '确认密码格式6-18个字母数字符号组合',
		'repassword.confirm' => '两次密码不一致',
		
		'platform.require' => '登录平台不能为空！',
		'platform.in' => '登录平台数值错误',
		
	];
	
	//自定义正则验证规则
	protected $regex = [
        'password'  => '^([a-zA-Z0-9]|[._\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\']){6,18}$',
    ];
	
	
		
	//自定义检测公司名重复
	protected function checkCompanyname($value,$rule,$data){
		$find = Db::name('system_company')->where(['name'=>$data['name']])->find();
		if($find){
			return '公司名称已存在';	
		}else{
			return true;
		}
	}
	
	
	//自定义手机验证码规则方法
	protected function checkVerify($value,$rule,$data){
		$verify = Db::name('system_verify')
				->where(['mobile'=>$data['mobile'],'status'=>0,'type'=>1])
				->order('create_at desc')
				->find();
		if(!$verify || $data['verify'] != $verify['code']){
			return '验证码错误';
		}		
		if(time() - $verify['create_at'] > 900){
			return '验证码已失效';
		}else{
			return true;
		}							
	}
	
	
	//验证场景
	protected $scene = [
		//注册
		'reg'   => ['mobile','name','password','platform'],
		//忘记密码修改
		'forgetpw_modify'  => ['mobile','password','repassword'],
		
	];
		
}
